package me.zhengjie.modules.sync.processing;

import com.alibaba.fastjson.JSON;
import com.qiaofang.applet.common.constant.SyncTypeEnum;
import com.qiaofang.applet.service.property.domain.PropertyRoles;
import com.qiaofang.applet.service.property.domain.PropertyVr;
import com.qiaofang.applet.service.property.domain.SyncDataQueue;
import com.qiaofang.applet.service.property.service.PropertyRolesService;
import com.qiaofang.applet.service.property.service.PropertyVrService;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.sync.ProcessingBase;
import me.zhengjie.utils.ImageUtil;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * v20房源角色人处理
 * @author QF2019
 */
@Slf4j
@Service
public class PropertyRoleSyncProcessing extends ProcessingBase {

    public PropertyRoleSyncProcessing(){
        setDataType(SyncTypeEnum.PRO_ROLE.getSyncType());
    }

    @Autowired
    private PropertyRolesService propertyRolesService;

    @Override
    protected void processing(SyncDataQueue syncDataQueue) {
        String batchContent = syncDataQueue.getBatchContent();
        List<PropertyRoles> list= JSON.parseArray(batchContent, PropertyRoles.class);
        if(null==list){
            log.info("processing PropertyRoles is null");
            return;
        }
        list.stream().forEach(item->{
            savePropertyRole(item,syncDataQueue);
        });
    }

    private void savePropertyRole(PropertyRoles item,SyncDataQueue syncDataQueue){
        PropertyRoles propertyRoles = propertyRolesService.findByPropertyUuidAndCompanyUuid(item.getPropertyUuid(), syncDataQueue.getCompanyUuid());

        if(!Objects.isNull(item.getDeleted()) && item.getDeleted()){
            if (!Objects.isNull(propertyRoles)) {
                propertyRolesService.deleteById(propertyRoles.getId());
            }
            return;
        }
        if(Objects.isNull(propertyRoles)){
            propertyRoles=new PropertyRoles();
        }
        //复制属性
        String[] ignoreProperties=new String[] {"id","createBy","updateBy","createTime","updateTime"};
        BeanUtils.copyProperties(item,propertyRoles,ignoreProperties);
        propertyRoles.setPropertyUniqueUuid(syncDataQueue.getCompanyUuid() + "_" + item.getPropertyUuid());
        propertyRoles.setCompanyUuid(syncDataQueue.getCompanyUuid());
        propertyRoles.setCityCode(syncDataQueue.getCityCode());
        propertyRoles.setCorporationCode(syncDataQueue.getCorporationCode());
        propertyRoles.setUpdateTime(new Date());
        propertyRoles.setLastSyncDate(new Date(System.currentTimeMillis() - DateUtil.DAY_MILLISECONDS));
        try {
            propertyRolesService.save(propertyRoles);
        } catch (Exception e) {
            log.error("savePropertyRole error",e);
            //记录错误日志
            this.saveErrorLog(JSON.toJSONString(propertyRoles), e.getMessage(), syncDataQueue);
        }
    }

}
